package org.feng.utils.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author by feng
 * @des redis通用
 * @date 2021/4/27 14:03
 */
@Component
public class RedisCommon {

    @Autowired
    protected RedisTemplate<Object, Object> redisTemplate;

    /**
     * 判断是否有当前key
     *
     * @param key
     * @return
     */
    public Boolean hasKey(Object key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param time
     */
    public void expireMill(Object key, Long time) {
        redisTemplate.expire(key, time, TimeUnit.MILLISECONDS);
    }

    /**
     * 获取过期时间
     *
     * @param key
     * @return
     */
    public Long getExpire(Object key) {
        return redisTemplate.getExpire(key);
    }

    /**
     * 删除key
     *
     * @param key
     */
    public void delete(Object key) {
        redisTemplate.delete(key);
    }

    /**
     * 获取key的type
     *
     * @param key
     * @return
     */
    public Object type(Object key) {
        return redisTemplate.type(key);
    }

    /**
     * 修改名称
     *
     * @param key
     * @param newKey
     */
    public void rename(Object key, Object newKey) {
        redisTemplate.rename(key, newKey);
    }

    /**
     * 移动到第几个数据库
     *
     * @param key
     * @param dataBaseIndex
     */
    public void move(Object key, Integer dataBaseIndex) {
        redisTemplate.move(key, dataBaseIndex);
    }
}
